package com.wl.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.haoyuban111.mubanapplication.R
import com.example.haoyuban111.mubanapplication.help_class.ContextHelper
import com.example.haoyuban111.mubanapplication.ui.view.RoundImageView
import com.wl.model.SecondModelKotlin
import java.util.*

class AdapterPopularLocationKotlin(mContext: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
//    private var mItemClickListener: OnItemClickListener? = null
    private val inflater: LayoutInflater = LayoutInflater.from(mContext)
    private var datas: MutableList<SecondModelKotlin>? = null
    var mItemClickListener: ((pos: Int) -> Unit)? = null

    fun setData(list: MutableList<SecondModelKotlin>) {
        if (datas == null) {
            datas = ArrayList<SecondModelKotlin>()
        }
        datas!!.clear()
        datas!!.addAll(list)
        notifyDataSetChanged()
    }

    val list: MutableList<SecondModelKotlin>
        get() = datas!!

    override fun getItemCount(): Int {
        return datas!!.size
    }


    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {

        val holder = viewHolder as ChildViewHolder
        val model = datas!![position]
        holder.setModel(model)

        holder.itemView?.setOnClickListener { mItemClickListener?.invoke(position) }


    }

    override fun onCreateViewHolder(viewHolder: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = inflater.inflate(R.layout.item_popular_country, null)
        val holder = ChildViewHolder(view)
        return holder
    }


//    fun setOnItemClickListener(listener: ((pos: Int) -> Unit)){
//        mItemClickListener = listener
//    }

    fun setOnItemClickListener(listener: ((pos: Int) -> Unit)){
        mItemClickListener = listener
    }

    fun onItemClick(pos: Int){}


    inner class ChildViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        internal var iv_image: RoundImageView? = null
        internal var tv_area: TextView? = null
        internal var model: SecondModelKotlin? = null
        internal var rl_parent: RelativeLayout

        init {
            iv_image = v.findViewById(R.id.iv_image) as RoundImageView
            tv_area = v.findViewById(R.id.tv_area) as TextView
            rl_parent = v.findViewById(R.id.rl_parent) as RelativeLayout
        }

        fun setModel(model: SecondModelKotlin) {
            Glide.with(ContextHelper.getApplicationContext())
                    .load(model.avatarUrl)
                    .centerCrop()
                    .error(R.drawable.avatar_default_black)
                    .into(iv_image)
            tv_area!!.text = model.name

        }


    }
}